package com.css.fxfzdzzh.modules.basicData.repository.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdzzh.constants.FxfzConstants;
import com.css.fxfzdzzh.constants.YNEnum;
import com.css.fxfzdzzh.modules.basicData.entity.SysDataVersion;
import com.css.fxfzdzzh.modules.basicData.param.PageQueryParam;
import com.css.fxfzdzzh.modules.basicData.param.SysDataVersionSameTypeDataExistParam;
import com.css.fxfzdzzh.modules.basicData.param.SysDataVersionSaveMetaDataInfoParam;
import com.css.fxfzdzzh.modules.basicData.param.sysDataVersion.SysDataVersionPageParam;
import com.css.fxfzdzzh.modules.basicData.vo.VersionVO;
import com.css.fxfzdzzh.modules.taskManage.entity.TaskEntity;
import com.css.fxfzdzzh.util.CreateGson;
import com.css.fxfzdzzh.util.PlatformObjectUtils;
import com.css.fxfzdzzh.web.PlatformSessionContext;
import com.css.fxfzdzzh.zcpt.sys.entity.SUser;
import com.css.fxfzdzzh.zcpt.sys.service.SUserService;
import com.css.fxfzdzzh.zcpt.sys.service.SysAreaService;
import com.css.fxfzdzzh.zcpt.sys.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年04月25日  12:17
 */
@Repository
public class SysDataVersionRepository {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    SUserService sUserService;
    @Resource
    SysRoleService sysRoleService;
    @Resource
    SysAreaService sysAreaService;



    private final static String TABLE_NAME = "dzzh_sys_data_version"; // 表明
    private final static String TABLE_ID_KEY = "id"; // 表id字段名

    public void save(SysDataVersion sysDataVersion) {
        String sql = "insert into " + TABLE_NAME +
                " values(" +
                ":id, " +
                ":versionCode, " +
                ":versionTime, " +
                ":versionDesc, " +
                ":attachmentIds, " +
                ":uploadUser, " +
                ":updateDesc, " +
                ":dataRangeCountry, " +
                ":dataRangeProvince," +
                ":dataRangeCity," +
                ":dataName," +
                ":dataDesc," +
                ":uploadTime," +
                ":dataTypeLevel1Code," +
                ":dataTypeLevel2," +
                ":delFlag," +
                ":dataFileIds," +
                ":dataRangeArea, " +
                ":dataTypeLevel3, " +
                ":uploadSource, " +
                ":isSpatialData, " +
                ":spatialDataType, " +
                ":projectionInfo, " +
                ":fieldNum, " +
                ":fieldEntry," +
                ":updateTime," +
                ":updateUser," +
                ":dataRangeStr," +
                ":createTime," +
                ":createUser," +
                ":uploadUserName" +
                " )";
        SqlParameterSource source = new BeanPropertySqlParameterSource(sysDataVersion);
        namedParameterJdbcTemplate.update(sql, source);
    }

    public List<String> getVersionCodeListByType( Map<String,Object> map) {
        StringBuilder sql = new StringBuilder("select version_code from " + TABLE_NAME + " where data_type_level2 = '" + map.get("dataType2") + "' and del_flag = '0' ");
        String userId =  PlatformSessionContext.getUserID();
        if(PlatformObjectUtils.isNotEmpty(map.get("province"))  && !"".equals(map.get("province"))){
            sql.append(" and data_range_province = '").append(map.get("province")).append("'");
        }
        if(PlatformObjectUtils.isNotEmpty(map.get("city")) && !"".equals(map.get("city"))){
            sql.append(" and data_range_city = '").append(map.get("city")).append("'");
        }
        if(PlatformObjectUtils.isNotEmpty(map.get("area")) && !"".equals(map.get("area"))){
            sql.append(" and data_range_area = '").append(map.get("area")).append("'");
        }
        if(PlatformObjectUtils.isNotEmpty(map.get("dataRangeCountry")) && !"".equals(map.get("dataRangeCountry"))){
            sql.append(" and data_range_country = '").append(map.get("dataRangeCountry")).append("'");
        }
        if(PlatformObjectUtils.isNotEmpty(map.get("versionCode")) && !"".equals(map.get("versionCode"))){
            sql.append(" and version_code = '").append(map.get("versionCode")).append("'");
        }
//        String user = sUserService.getSUser(userId);
//        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
//        String role = sysRoleService.getRoleByUserId(userId);
//        List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
//        StringBuilder str=new StringBuilder("");
//        for(Map roleMap:roleMaps){
//            str.append(roleMap.get("roleCode")+",");
//        }
//        String roles = str.toString();
//        if(roles.contains(FxfzConstants.YPG_COUN_GL_ROLE) || roles.contains(FxfzConstants.YPG_COUN_YW_ROLE)){
//            //国家级管理员和国家级业务人员可以看国家级数据
//            sql.append(" and data_range_country = '1'");
//        }else if(roles.contains(FxfzConstants.YPG_PRI_GL_ROLE) || roles.contains(FxfzConstants.YPG_PRI_YW_ROLE)){
//            // 省级管理员和省级业务人员可以全看自己省和国家级的
//            sql.append(" and (data_range_country = '1' or data_range_province = '"+sUser.getProvinceId()+"')");
//        }
        sql.append(" order by create_time desc");
        return jdbcTemplate.queryForList(sql.toString(), String.class);
    }

    public void saveMetaDataInfo(SysDataVersionSaveMetaDataInfoParam param) {
        String sql = "update " + TABLE_NAME + " set " +
                "upload_source = :uploadSource," +
                "spatial_data_type = :spatialDataType," +
                "projection_info = :projectionInfo," +
                "field_num = :fieldNum," +
                "field_entry = :fieldEntry," +
                "update_time = :updateTime," +
                "update_user = :updateUser" +
                " where version_code" + " = :versionCode";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    public SysDataVersion getByVersionCode(String versionCode) {
        String sql = "select * from " + TABLE_NAME + " where version_code = '" + versionCode + "' and del_flag='0'";
        List<SysDataVersion> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SysDataVersion.class));
        return query.size()<1?null:query.get(0);
    }

    public void realDelByVersionCode(String versionCode) {
        jdbcTemplate.update(new StringBuilder("delete from " + TABLE_NAME + " where version_code" + " = '")
                .append(versionCode).append("' and del_flag='0'").toString());
    }

    public void delByIds(List<String> ids, String delUser, Date delTime) {
        String format = DateUtil.format(delTime, "yyyy-MM-dd HH:mm:ss");
        String sql = "update " + TABLE_NAME + " set del_flag = '1', update_time = '" + format + "', update_user = '" + delUser + "' where id in ('" + StringUtils.join(ids, "','") + "')";
        jdbcTemplate.update(sql);
    }

    public List<SysDataVersion> page(SysDataVersionPageParam sysDataVersionPageParam) {
        StringBuilder sqlBuilder = new StringBuilder("select * from " + TABLE_NAME + " t where 1=1 ");
        pageSQLBuilder(sysDataVersionPageParam, sqlBuilder);
        sqlBuilder.append("and t.del_flag = '0'");
        sqlBuilder.append(" order by t.create_time desc");
        if (sysDataVersionPageParam.getPage() != null && sysDataVersionPageParam.getRows() != null) {
            this.pageSQLAppend(sqlBuilder, sysDataVersionPageParam);
        }
        return jdbcTemplate.query(sqlBuilder.toString(), new BeanPropertyRowMapper<>(SysDataVersion.class));
    }

    private void pageSQLAppend(StringBuilder sqlBuilder, SysDataVersionPageParam basePageParam) {
        sqlBuilder.append(" limit ").append(basePageParam.getRows()).append(" offset ").append(basePageParam.getPage());
    }

    private void pageSQLBuilder(SysDataVersionPageParam param, StringBuilder sqlBuilder) {
        if (param.getVersionStartTime() != null && param.getVersionEndTime() != null) {
            sqlBuilder.append("and t.version_time between '").append(DateUtil.format(param.getVersionStartTime(), "yyyy-MM-dd")).append("' and '").append(DateUtil.format(param.getVersionEndTime(), "yyyy-MM-dd")).append("' ");
        }
        if (param.getUploadStartTime() != null && param.getUploadEndTime() != null) {
            sqlBuilder.append("and t.upload_time between '").append(DateUtil.format(param.getUploadStartTime(), "yyyy-MM-dd")).append("' and '").append(DateUtil.format(param.getUploadEndTime(), "yyyy-MM-dd")).append("' ");
        }
        if (StringUtils.isNotBlank(param.getVersionCode())) {
            sqlBuilder.append("and t.version_code like '%").append(param.getVersionCode().trim()).append("%' ");
        }
        if (StringUtils.isNotBlank(param.getDataName())) {
            sqlBuilder.append("and t.data_name like '%").append(param.getDataName().trim()).append("%' ");
        }
        if (StringUtils.isNotBlank(param.getDataType())) {
            sqlBuilder.append("and t.data_type_level2 = '").append(param.getDataType()).append("' ");
        }
        if (param.getDataRangeCountry() != null) {
            sqlBuilder.append("and t.data_range_country = '").append(param.getDataRangeCountry()).append("' ");
        }
        if (StringUtils.isNotBlank(param.getDataRangeProvince())) {
            sqlBuilder.append("and t.data_range_province = '").append(param.getDataRangeProvince()).append("' ");
//            if (param.getDataRangeProvince().equals("-1")) {
//                sqlBuilder.append("and t.data_range_province is null ");
//            } else {
//                if (param.getHasNationwide() == 0) {
//                    sqlBuilder.append("and t.data_range_province = '").append(param.getDataRangeProvince()).append("' ");
//                } else {
//                    sqlBuilder.append("and (t.data_range_province = '").append(param.getDataRangeProvince()).append("' ").append(" or t.data_range_province is null ").append(") ");
//                }
//            }
        }
        if (StringUtils.isNotBlank(param.getDataRangeCity())) {
            sqlBuilder.append("and t.data_range_city = '").append(param.getDataRangeCity()).append("' ");
//            if (param.getHasNationwide() == 0) {
//
//            } else {
//                sqlBuilder.append("and (t.data_range_city = '").append(param.getDataRangeCity()).append("' ").append(" or t.data_range_city is null ").append(") ");
//            }
        }
        if (StringUtils.isNotBlank(param.getDataRangeArea())) {
            sqlBuilder.append("and t.data_range_area = '").append(param.getDataRangeArea()).append("' ");
//            if (param.getHasNationwide() == 0) {
//
//            }
//            else {
//                sqlBuilder.append("and (t.data_range_area = '").append(param.getDataRangeArea()).append("' ").append(" or t.data_range_area is null ").append(") ");
//            }
        }
        if (StringUtils.isNotBlank(param.getDataTypeCode())) {
            sqlBuilder.append("and t.data_type_level1_code = '").append(param.getDataTypeCode()).append("' ");
        }
        if (StringUtils.isNotBlank(param.getVersionDesc())) {
            sqlBuilder.append(" and t.version_desc like '%").append(param.getVersionDesc().trim()).append("%' ");
        }
        if (StringUtils.isNotBlank(param.getUploadUserIds())) {
            sqlBuilder.append(" and t.upload_user in ('").append(param.getUploadUserIds()).append("')");
        }
    }

    public Integer countForPage(SysDataVersionPageParam sysDataVersionPageParam) {
        StringBuilder sqlBuilder = new StringBuilder("select count(*) from " + TABLE_NAME + " t where 1=1 ");
        pageSQLBuilder(sysDataVersionPageParam, sqlBuilder);
        sqlBuilder.append("and t.del_flag = '0'");
        return jdbcTemplate.queryForObject(sqlBuilder.toString(), Integer.class);
    }

    public List<SysDataVersion> getDataVersionView(SysDataVersionPageParam param) {
        StringBuilder sqlBuilder = new StringBuilder("select * from " + TABLE_NAME + " where 1=1 ");
        if (StringUtils.isNotBlank(param.getDataTypeCode())){
            sqlBuilder.append("and data_type_level1_code = '").append(param.getDataTypeCode()).append("' ");
        }
        if (StringUtils.isNotBlank(String.valueOf(param.getDataRangeCountry()))) {
            sqlBuilder.append("and data_range_country = '").append(param.getDataRangeCountry()).append("' ");
        }
        if (StringUtils.isNotBlank(param.getDataRangeProvince())) {
            sqlBuilder.append("and data_range_province = '").append(param.getDataRangeProvince()).append("' ");
        }
        if (StringUtils.isNotBlank(param.getDataRangeCity())) {
            sqlBuilder.append("and data_range_city = '").append(param.getDataRangeCity()).append("' ");
        }
        if (StringUtils.isNotBlank(param.getDataRangeArea())) {
            sqlBuilder.append("and data_range_area = '").append(param.getDataRangeArea()).append("' ");
        }
        if (StringUtils.isNotBlank(param.getVersionCode())){
            sqlBuilder.append("and version_code like  '%").append(param.getVersionCode()).append( "%' ");

        }
        return jdbcTemplate.query(sqlBuilder.toString(), new BeanPropertyRowMapper<>(SysDataVersion.class));

    }

    /**
     * 判断是否已有同类数据
     * 判断依据：如果选择的一样的数据类别且一样的数据范围
     *
     * @param sysDataVersionSameTypeDataExistParam
     * @return
     */
    public String sameTypeDataExist(SysDataVersionSameTypeDataExistParam sysDataVersionSameTypeDataExistParam) {
        StringBuilder sqlBuilder = new StringBuilder("select version_code from " + TABLE_NAME + " where 1=1 ");
        sqlBuilder.append(" and data_type_level2 = '").append(sysDataVersionSameTypeDataExistParam.getDataTtypeLevel2()).append("'");
        sqlBuilder.append(" and data_range_country = ").append(sysDataVersionSameTypeDataExistParam.getDataRangeCountry());
        if (StringUtils.isNotBlank(sysDataVersionSameTypeDataExistParam.getDataRangeProvince())) {
            sqlBuilder.append(" and data_range_province = '").append(sysDataVersionSameTypeDataExistParam.getDataRangeProvince()).append("'");
        }
        if (StringUtils.isNotBlank(sysDataVersionSameTypeDataExistParam.getDataRangeCity())) {
            sqlBuilder.append(" and data_range_city = '").append(sysDataVersionSameTypeDataExistParam.getDataRangeCity()).append("'");
        }
        if (StringUtils.isNotBlank(sysDataVersionSameTypeDataExistParam.getDataRangeArea())) {
            sqlBuilder.append(" and data_range_area = '").append(sysDataVersionSameTypeDataExistParam.getDataRangeArea()).append("'");
        }
        sqlBuilder.append(" and del_flag = '0'");
        sqlBuilder.append(" order by upload_time desc");
        sqlBuilder.append(" limit ").append(1).append(" offset ").append(0);
        try {
            return jdbcTemplate.queryForObject(sqlBuilder.toString(), String.class);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public List<String> selectAllUploadUserId() {
        return jdbcTemplate.queryForList("SELECT upload_user from sys_data_version GROUP BY upload_user;", String.class);
    }

    public List<SysDataVersion> getVersionInfo(SysDataVersionPageParam param, String geom) {
        StringBuilder sql = new StringBuilder("select * " + (geom == null ? ",ST_AsText(geom) geom" : ",ST_AsText(St_intersection(st_geometryfromtext(" + geom + ",4490),geom)) geom") + " from " + TABLE_NAME + " where del_flag = '0'" + (geom == null ? "" : " and st_intersects(st_geomfromtext(" + geom + ",4490),geom)"));
        if (PlatformObjectUtils.isNotEmpty(param.getDataTypeCode())){
            sql.append(" and data_type_level1_code = '").append(param.getDataTypeCode()).append("'");
        }
        if (StringUtils.isNotBlank(param.getVersionDesc())) {
            sql.append(" and version_desc like '%").append(param.getVersionDesc().trim()).append("%' ");
        }
        if (StringUtils.isNotBlank(param.getUploadUserIds())) {
            sql.append(" and upload_user in ('").append(param.getUploadUserIds()).append("')");
        }
        if (param.getUploadStartTime() != null && param.getUploadEndTime() != null) {
            sql.append("and upload_time between '").append(DateUtil.format(param.getUploadStartTime(), "yyyy-MM-dd")).append("' and '").append(DateUtil.format(param.getUploadEndTime(), "yyyy-MM-dd")).append("' ");
        }
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SysDataVersion.class));
    }

    public SysDataVersion selectOneByType2(String dataTypeLevel1Code, String dataTypeLevel2) {
        StringBuilder sql = new StringBuilder("select * from ").append(TABLE_NAME).append(" where del_flag='").append(YNEnum.N).append("'");
        sql.append(" and data_type_level1_code='").append(dataTypeLevel1Code).append("'").append(" and data_type_level2='").append(dataTypeLevel2).append("'");
        List<SysDataVersion> query = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SysDataVersion.class));
        return PlatformObjectUtils.isNotEmpty(query)?query.get(0):null;
    }

    public List<SysDataVersion> selectByIds(List<String> ids) {
        StringBuilder sqlBuilder = new StringBuilder("SELECT * from " + TABLE_NAME + " where " + TABLE_ID_KEY + " IN ('");
        for (String id : ids) {
            sqlBuilder.append(id).append("', '");
        }
        sqlBuilder.append("') order by create_time desc");
        return jdbcTemplate.query(sqlBuilder.toString().replaceAll("\"", ""), new BeanPropertyRowMapper<>(SysDataVersion.class));
    }

    public List<SysDataVersion> getVersionList(Map<String, Object> map) {
        StringBuilder sql = new StringBuilder("select * from " + TABLE_NAME + " where data_type_level2 = '" + map.get("dataType2") + "' and del_flag = '0' ");
        if(PlatformObjectUtils.isNotEmpty(map.get("province")) && !"".equals(map.get("province"))){
            sql.append(" and data_range_province = '").append(map.get("province")).append("'");
        }
        if(PlatformObjectUtils.isNotEmpty(map.get("city")) && !"".equals(map.get("city"))){
            sql.append(" and data_range_city = '").append(map.get("city")).append("'");
        }
        if(PlatformObjectUtils.isNotEmpty(map.get("area")) && !"".equals(map.get("area"))){
            sql.append(" and data_range_area = '").append(map.get("area")).append("'");
        }
        if(PlatformObjectUtils.isNotEmpty(map.get("dataRangeCountry")) && !"".equals(map.get("dataRangeCountry"))){
            sql.append(" and data_range_country = '").append(map.get("dataRangeCountry")).append("'");
        }
        if(PlatformObjectUtils.isNotEmpty(map.get("versionCode")) && !"".equals(map.get("versionCode"))){
            sql.append(" and version_code like '%").append(map.get("versionCode")).append("%'");
        }
        sql.append(" order by create_time desc");
        return jdbcTemplate.query(sql.toString(),new BeanPropertyRowMapper<>(SysDataVersion.class));
    }

    public Map<String, Object> getYwVesrionData(PageQueryParam pageQueryParam) {
        StringBuilder sql = new StringBuilder("select version_time,version_code,data_range_str dataRange,upload_time,upload_user_name uploadUser,data_name,version_desc,data_desc,data_file_ids,attachment_ids,data_type_level2 dataTypeLevel3 from dzzh_sys_data_version");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '"+ YNEnum.N.toString() +"'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        StringBuilder countSql = new StringBuilder(" select count(*) from dzzh_sys_data_version");
        int firstIndex = (pageQueryParam.getPage() - 1) * pageQueryParam.getRows();
        int lastIndex = pageQueryParam.getRows();

        String distinct = pageQueryParam.getDistinct();
        if(StringUtils.isNotBlank(distinct)){
            //distinct不为空表示不跨省，可选国家级-全国、任务所在省
            String areaId = sysAreaService.getIdByCondition(URLEncoder.encode(distinct),"","");
            whereSql.append(" and (data_range_country = '1' or (data_range_country = '2' and data_range_province='").append(areaId).append("'))");
//            //根据任务区域确定要查询的业务数据的版本范围
//            String[] split = distinct.split("、");
//            int length = split.length;
//            for(int i=0;i<length;i++){
//                if(i==0){
//                    whereSql.append(" and ( ");
//                }else{
//                    whereSql.append(" or ");
//                }
//                String region = split[i];
//                String[] regionSplit = region.split("-");
//                int regionLength = regionSplit.length;
//                String province = "";
//                String city = "";
//                String area = "";
//                if(regionLength==1){
//                    //表示某个省
//                    province = URLEncoder.encode(regionSplit[0]);
//                }else if(regionLength==2){
//                    //省-市/直辖市-区
//                    province = URLEncoder.encode(regionSplit[0]);
//                    if("北京市".equals(province) || "上海市".equals(province) || "天津市".equals(province) || "重庆市".equals(province)){
//                        area = URLEncoder.encode(regionSplit[1]);
//                    }else{
//                        city = URLEncoder.encode(regionSplit[1]);
//                    }
//
//                }else if(regionLength==3){
//                    //省-市-区县
//                    //省-市/直辖市-区
//                    province = URLEncoder.encode(regionSplit[0]);
//                    city = URLEncoder.encode(regionSplit[1]);
//                    area = URLEncoder.encode(regionSplit[2]);
//                }
//                String areaId = sysAreaService.getIdByCondition(province,city,area);
//                if(StringUtils.isNotBlank(areaId)){
//                    String[] idArr = areaId.split("_");
//                    int idLength = idArr.length;
//                    if(idLength==1){
//                        whereSql.append( "  (data_range_province='").append(idArr[0]).append("')");
//                    }else if(idLength==2){
//                        whereSql.append( "  (data_range_province='").append(idArr[0]).append("'");
//                        whereSql.append( " and data_range_city='").append(idArr[1]).append("')");
//                    }else if(idLength==3){
//                        whereSql.append( "  (data_range_province='").append(idArr[0]).append("'");
//                        whereSql.append( " and data_range_city='").append(idArr[1]).append("'");
//                        whereSql.append( " and data_range_area='").append(idArr[2]).append("')");
//                    }
//                }
//                if(i==length-1){
//                    whereSql.append(" )");
//                }
//            }
//            StringBuilder str = new StringBuilder("");
//            String userId = null;
//            if (PlatformObjectUtils.isEmpty(pageQueryParam.getUserId())){
//                userId = "80cd530287e54c6788925adf2dfc9da1";
//            }else {
//                userId = pageQueryParam.getUserId();
//            }
//            String user = sUserService.getSUser(userId);
//            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
//            String role = sysRoleService.getRoleByUserId(userId);
//            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
//            for (Map roleMap : roleMaps) {
//                str.append(roleMap.get("roleCode")).append(",");
//            }
//            String roles = str.toString();
//            if (roles.contains(FxfzConstants.DZZH_COUN_GL_ROLE) || roles.contains(FxfzConstants.DZZH_COUN_YW_ROLE)) {
//                whereSql.append(" and (data_range_country = '1' or data_range_country = '2')");
//            } else if (roles.contains(FxfzConstants.DZZH_PRI_GL_ROLE) || roles.contains(FxfzConstants.DZZH_PRI_YW_ROLE)) {
//                whereSql.append(" and data_range_country = '2'");
//            }
        }else{
            //district 为空，表示跨省，任务范围跨省的：可选国家级-全国。
            whereSql.append(" and data_range_country = '1'");
        }
        if (!PlatformObjectUtils.isEmpty(pageQueryParam.getVersionDesc())) {
            whereSql.append(" and version_desc like '%").append(pageQueryParam.getVersionDesc()).append("%'");
        }
        if (!PlatformObjectUtils.isEmpty(pageQueryParam.getUploadUsername())) {
            whereSql.append(" and upload_user_name like '%").append(pageQueryParam.getUploadUsername()).append("%'");
        }
        if (pageQueryParam.getUploadStartTime() != null && pageQueryParam.getUploadEndTime() != null) {
            whereSql.append(" and upload_time between '").append(pageQueryParam.getUploadStartTime()).append("' and '").append(pageQueryParam.getUploadEndTime()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(pageQueryParam.getDataTypeLevel3())) {
            whereSql.append(" and data_type_level2 = '").append(pageQueryParam.getDataTypeLevel3()).append("'");
        }
        String sqlWhole = sql.append(whereSql).append(orderSql).append(" limit ").append(lastIndex).append(" offset ").append(firstIndex).toString();
        countSql.append(whereSql);
        List<VersionVO> list = jdbcTemplate.query(sqlWhole, new BeanPropertyRowMapper<>(VersionVO.class));
        Integer total = jdbcTemplate.queryForObject(countSql.toString(), Integer.class);
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }
}
